package com.yqya.springboot.shiro.practice.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yqya.springboot.shiro.practice.dto.UserInfoDTO;

/**
 * 用户登录
 *
 * @author Kevin
 * @date 2019-07-28
 */
@RestController
@RequestMapping("/user_login")
public class UserLoginController {

    /**
     * 用户登录
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/login")
    public String login(@RequestBody UserInfoDTO dto) {
        // 添加用户认证信息
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(dto.getName(), dto.getPwd());
        // 进行验证，这里可以捕获异常，然后返回对应信息
        SecurityUtils.getSubject().login(usernamePasswordToken);
        return "登录成功";
    }

    /**
     * 退出的时候是get请求，主要是用于退出
     */
    @GetMapping(value = "/login")
    public String login() {
        return "login";
    }

    @GetMapping(value = "/logout")
    public String logout() {
        return "logout";
    }

}
